package com.lst.api.controller;

import com.lst.api.service.ILoanApplyService;
import com.lst.api.vo.request.LoanApplyRequest;
import com.lst.api.vo.response.LoanApplyResponse;
import com.lst.api.vo.response.LoanApplySelectDataResponse;
import com.lst.tools.custom.annotation.TokenPermission;
import com.lst.tools.custom.context.BaseContextHandler;
import com.lst.tools.custom.vo.JsonResponse;
import com.lst.tools.web.annotation.WebLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Api(tags = "贷款申请控制层")
@RestController
@RequestMapping(value = "/loanApply")
public class LoanApplyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoanApplyController.class);

    @Autowired
    private ILoanApplyService loanApplyService;

    @ApiOperation(value = "申请贷款")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "access-token", value = "登录token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "body", name = "loanApplyRequest", value = "请求对象", required = true, dataType = "LoanApplyRequest")
    })
    @PostMapping(value = "/apply")
    @TokenPermission
    @WebLog
    public JsonResponse<String> apply(@RequestBody LoanApplyRequest loanApplyRequest) {
        Long userId = BaseContextHandler.getUserId();
        String applyNo = loanApplyService.apply(userId, loanApplyRequest);
        LOGGER.info("Function[apply] apply success. userId:{}", userId);
        return JsonResponse.success(applyNo);
    }
    
    @ApiOperation(value = "查询是否身份证号是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "access-token", value = "登录token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "idCardNo", value = "身份证号", required = true, dataType = "String")
    })
    @GetMapping(value = "/idCardNoIsExist")
    @TokenPermission
    @WebLog
    public JsonResponse<Boolean> idCardNoIsExist(@RequestParam(value = "idCardNo") String idCardNo) {
        boolean idCardNoIsExist = loanApplyService.idCardNoIsExist(idCardNo);
        return JsonResponse.success(idCardNoIsExist);
    }

    @ApiOperation(value = "根据身份证号获取申请数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "access-token", value = "登录token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "idCardNo", value = "身份证号", required = true, dataType = "String")
    })
    @GetMapping(value = "/getInfoByIdCardNo")
    @TokenPermission
    @WebLog
    public JsonResponse<LoanApplyResponse> getInfoByIdCardNo(@RequestParam(value = "idCardNo") String idCardNo) {
        LoanApplyResponse loanApplyResponse = loanApplyService.getInfoByIdCardNo(idCardNo);
        return JsonResponse.success(loanApplyResponse);
    }

    @ApiOperation(value = "获取全部地址信息")
    @GetMapping(value = "/listAllSelectData")
    public JsonResponse<LoanApplySelectDataResponse> listAllSelectData() {
        LoanApplySelectDataResponse loanApplySelectDataResponse = loanApplyService.listAllSelectData();
        return JsonResponse.success(loanApplySelectDataResponse);

    }

}